package com.atguigu.gmall.weball.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.cart.CartFeignClient;
import com.atguigu.gmall.model.cart.vo.AddSuccessVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author:lyd
 **/
@Controller
public class CartController {

    @Autowired
    CartFeignClient cartFeignClient;

    //    public static final ThreadLocal<HttpServletRequest> requestThreadLocal = new ThreadLocal<>();
    @GetMapping("/addCart.html")
    public String addCart(@RequestParam("skuId") Long skuId,
                          @RequestParam("skuNum") Long skuNum,
                          HttpServletRequest request,
                          Model model) {
//        requestThreadLocal.set(request);
        Result<AddSuccessVo> result = cartFeignClient.addCart(skuId, skuNum);
        if (result.getCode() == 200) {
            model.addAttribute("skuInfo", result.getData().getSkuInfo());
            model.addAttribute("skuNum", result.getData().getSkuNum());
        }else {
            model.addAttribute("msg",result.getMessage());
        }
//        requestThreadLocal.remove();
        return "cart/addCart";
    }

    @GetMapping("/cart.html")
    public String cartList() {
        return "cart/index";
    }

    @GetMapping("/cart/deleteChecked")
    public String deleteChecked() {
        cartFeignClient.deleteChecked();
        return "redirect:http://cart.gmall.com/cart.html";
    }
}
